package adv41;

import adv331.Sex;
import adv331.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author oldliu
 * @since 1.0
 */
public class StuTest {
    public static void main(String[] args) throws  Exception {
        Class<Student> s1 = Student.class;
        //无参构造
        Student stu= s1.getDeclaredConstructor().newInstance();
        System.out.println(stu);
        System.out.println("有参构造方法：");
        Constructor<Student> cr = s1.getDeclaredConstructor(String.class, int.class, Sex.class,
                String.class);
        Student student = cr.newInstance("为建州", 22, Sex.妖, "2000343");
        System.out.println(student);
        //私有的构造方法都可以
        System.out.println("有参构造方法：");
        Constructor<Student> cr2 = s1.getDeclaredConstructor(int.class, String.class, Sex.class,
                String.class);
        cr2.setAccessible(true);//取消私有限制
        Student student2 = cr2.newInstance( 22, "杨旭",Sex.妖, "2000343");
        System.out.println(student2);
        //获得私有属性
        System.out.println("修改私有属性：：：");
        Field fage=s1.getDeclaredField("age");
        fage.setAccessible(true);
        System.out.println("age属性的值是:"+fage.getInt(student2));
        fage.set(student2,29);
        System.out.println(student2);
        System.out.println("私有方法不放过");
        Method ma=s1.getDeclaredMethod("incr",int.class);
        ma.setAccessible(true);
        Object invoke = ma.invoke(student2, 5);
        System.out.println("方法返回了："+invoke);
        System.out.println(student2);
    }
}
